import asyncio

from loguru import logger

from com.arcfox.base.base_spider import ExecuteType
from com.arcfox.school.processor.yzy.yzy_province_score_processor import YZYProvinceScoreProcessor
from com.arcfox.school.processor.yzy.yzy_school_list_processor import YZYSchoolListProcessor
from com.arcfox.school.spider.yzy.yzy_base_spider import YZYBaseSpider
from com.arcfox.util import async_request as requests
from com.arcfox.util.util import random_sleep


class YZYProvinceScoreSpider(YZYBaseSpider):
    '''
    优志愿学校列表抓取, 用列表数据抓取学校相关的图片, 以及学校编码更新到tb_university_info
    '''

    def __init__(self):
        super().__init__()
        self.url = "https://uwf7de983aad7a717eb.youzy.cn/youzy.dms.datalib.api.enrolldata.pcl.query"
        self.start_page = 1
        self.processor = YZYProvinceScoreProcessor()

    def get_params(self, p_code):
        return '{"provinceCode": '+str(p_code)+', "year": 0}'

    def get_headers(self, params):
        return {
            "Host": "uwf7de983aad7a717eb.youzy.cn",
            "u-sign": self.get_u_sgin(params),
            "Content-Type": "application/json",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
            "Referer": "https://pv4y-pc.youzy.cn/",
        }

    async def _pull_task(self):
        return None, ExecuteType.ONCE

    async def _crawl_by_task(self, task):
        p_map = self.get_province_map_reverse()
        for p_code in p_map:
            params = self.get_params(p_code)
            resp = await requests.post_with_session(url=self.url, data=params, headers=self.get_headers(params))
            if resp.code == 200:
                result_json = resp.json()
                logger.info(result_json)
                await self.processor.parse_and_save_data(result_json)


if __name__ == "__main__":
    asyncio.run(YZYProvinceScoreSpider().start_crawl())
